package scuts.core.classes;

import scuts.core.types.Ordering;

//@:typeClass
class Ord<T> {
  
  var equals:Eq<T>;
  
  public function new (equals:Eq<T>) {
    this.equals = equals;
  }
  
  public function compare (a:T, b:T):Ordering {
    return if      (equals.eq(a, b))  Ordering.EQ
           else if (lessOrEq(a, b))   Ordering.LT
           else                       Ordering.GT;
  }
  
  public function less (a:T, b:T):Bool {
    return compare(a, b) == LT;
  }
  
  public function lessOrEq (a:T, b:T):Bool {
    return compare(a, b) != GT;
  }
  
  public function greaterOrEq (a:T, b:T):Bool {
    return compare(a, b) != LT;
  }
  
  public function greater (a:T, b:T):Bool {
    return compare(a, b) == GT;
  }
  
  public function min (a:T, b:T):T {
    return lessOrEq(a, b) ? a : b;
  }
  
  public function max (a:T, b:T):T {
    return greaterOrEq(a, b) ? a : b;
  }
  
}
